package gps

import (
	"math"
)

const earthRadius float64 = 6378.137

//Rad 角度转换为弧度
func Rad(d float64) float64 {
	return d * math.Pi / 180.0
}

//Distance 计算两点经纬度距离
func Distance(lon1, lat1, lon2, lat2 float64) float64 {
	radLat1 := Rad(lat1)
	radLat2 := Rad(lat2)
	a := radLat1 - radLat2
	b := Rad(lon1) - Rad(lon2)
	s := 2 * math.Asin(math.Sqrt(math.Pow(math.Sin(a/2), 2)+math.Cos(radLat1)*math.Cos(radLat2)*math.Pow(math.Sin(b/2), 2)))
	s = s * earthRadius
	s = s * 1000
	return s
}

//GetPoint 根据方位角和距离计算经纬度
func GetPoint(lon1 float64, lat1 float64, azimuth float64, r float64) (lon, lat float64) {
	const ARC = 6371393
	radLat1 := Rad(lat1)
	radAlpha := Rad(azimuth)

	lon2 := lon1 + r*math.Sin(radAlpha)/(ARC*math.Cos(radLat1)*2*math.Pi/360)
	lat2 := lat1 + r*math.Cos(radAlpha)/(ARC*2*math.Pi/360)
	return lon2, lat2
}
